package com.changanw.repeat_submit.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisCache {
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 像redis中设置数据的方法
     * @param key  key
     * @param value  value
     * @param timeout  超时时间
     * @param timeUnit 单位
     * @param <T> 类型
     */
    public <T> void setCacheObject(final String key, T value, Integer timeout, final TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,value,timeout,timeUnit);
    }

    /**
     * 获取redis中的值
     * @param key
     * @param <T>
     * @return
     */
    public <T> T getCacheObject(final String key){
        ValueOperations<String,T> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }
}
